package TUILayer;
import java.util.Scanner;
import CtrLayer.*;
import ModelLayer.*;
import java.util.ArrayList;

/**
 * SalesUI is the class responsible for handling the TUI of the sales UI.
 * 
 * @author Gruppe 3 
 * @version 21-12-2011
 */
public class SalesUI
{
    private OrderCtr orderCtr; 
    private RentCtr rentCtr; 
    private PersonCtr personCtr; 
    private ItemCtr itemCtr;

    /**
     * Constructs an object of the class SalesUI and creates and initialize orderCtr, rentCtr, itemCtr and personCtr.
     */

    public SalesUI()
    {

        orderCtr = new OrderCtr();
        rentCtr = new RentCtr();
        personCtr = new PersonCtr();
        itemCtr = new ItemCtr();
    }

    /**
     * Starts the sales menu.
     */

    public void start()
    {
        salesMenu();
    }

    /**
     * Writes the sales menu. 
     */

    private int writeSalesMenu()
    {
        boolean ok = false;
        int choice = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Salgs Menu ***");
            System.out.println(" (1) Fortag salg ");
            System.out.println(" (2) Gå tilbage til hoved menu ");
            System.out.print("\n Tast dit valg: ");
            try
            {
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                printCatch();
                returnToMenu();
            }
        }
        return choice;
    }

    /**
     * Prints Sales Menu to terminalwindow and accepts input (choices).
     */

    private void salesMenu()
    {
        boolean exit = false;
        while(!exit)
        {

            int choice = writeSalesMenu(); // show menu and return user's choice

            if (choice == 1)
            {
                exit = createSale();
            }
            else if (choice == 2)
            {
                //mainUI.start();
                exit = true;
            }
            choice = 0;
        }
    }

    /**
     * Creates a new sale.
     */

    private boolean createSale()
    {
        boolean finished = false;
        boolean done = false;
        boolean exit = false;
        boolean exit3 = false;
        Person employee = null;
        Person customer = null;
        Order order = null;
        
            try
            {
                String userName = inputUserName();
                String password = inputPassword();
                employee = personCtr.logOn(userName, password);
            }
            catch(Exception e)
            {
                System.out.println("");  
                System.out.println("Fejl opstod: Brugernavn eller kodeord er forkert eller");
                System.out.println("der kan ikke findes en ansat i systemet");
                System.out.println("Fix ved at prøve igen med gyldig brugernavn og kodeord eller");
                System.out.println("gå tilbage og opret en ansat.");
                returnToMenu();
                exit = true;
                return exit;
        }

        try
        {
            String phoneNumber = inputPhoneNumber();
            int customerId = inputCustomerId();
            customer = personCtr.findCustomer(phoneNumber, customerId);

        }
        catch(Exception e)
        {
            System.out.println("");  
            System.out.println("Fejl opstod: Telefon nummer kunde ID er enten forkert eller");
            System.out.println("kunden er ikke oprettet");
            System.out.println("Evt. fix ved at prøve igen med et gyldigt telefon nummer og/eller kunde ID");
            System.out.println("eller gå tilbage og opret en kunde.");
            exit = true;
            return exit;
        }

        
        try
        {
            double discount = inputDiscount();
            order = orderCtr.createOrder(discount, employee, customer);
        }
        catch(Exception e)
        {
            System.out.println("");
            System.out.println("Fejl opstod: Discount skal være et % tal");
            returnToMenu();
            exit = true;
            return exit;
        }

        while(!finished)
        {
            try
            {
                String barCode = inputBarCode();
                int quantity = inputQuantity();
                double discount2 = inputDiscount();
                itemCtr.scanBarCode(barCode, quantity, discount2, order);
                System.out.println("");
                finished = inputFinished();
            }
            catch (Exception e)
            {
                System.out.println("");
                System.out.println("Fejl opstod: Enten er stregkode forkert eller discount skal være et tal");
                returnToMenu();
                exit = true;
                return exit;
            }
        }

        System.out.println("Total pris: " + orderCtr.endSale() + " kr.");
        double amount = inputAmount();
        System.out.println("");
        System.out.println("Bytte penge: " + orderCtr.makePayment(amount) + " kr.");
        order = orderCtr.getOrder();
        System.out.println("");
        System.out.println("Order ID: " + order.getOrderId());
        System.out.println("Kunde ID: " + customer.getId());
        System.out.println("Ekspederet af: " + employee.getName() + " " + employee.getId());
        System.out.println("Dato og tid for køb: " + order.getDate());
        System.out.println("********");
        ArrayList<PartOrder> partOrders = order.getPartOrders();
        for(PartOrder partOrder : partOrders)
        {

            System.out.println(partOrder.getItemName() + " antal: " + partOrder.getQuantity() + " Pris: " + partOrder.getPrice() + " Kr. Rabat: -" + partOrder.getDiscountInMoney() + " kr.");

        }
        System.out.println("********");
        System.out.println("Total pris i alt: " + order.getTotalPrice() + "kr. ");
        System.out.println("Discount på ordren: -" + order.getDiscountInMoney() + " kr.");
        boolean exit2 = inputFinished();
        if(exit2)
        {
            exit = true;
        }

        return exit;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return userName, the username of the employee.
     */

    private String inputUserName()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast brugernavn: ");
        String userName = keyboard.nextLine();
        return userName;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return password, the password of the employee.
     */

    private String inputPassword()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast adgangskode: ");
        String password = keyboard.nextLine();
        return password;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return phoneNumber, the phone number of the customer. 
     */

    private String inputPhoneNumber()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast kundens telefon nummer: ");
        String phoneNumber = keyboard.nextLine();
        return phoneNumber;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return customerId, the customer ID of the customer. 
     */

    private int inputCustomerId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast kundens kunde ID: ");
        int customerId = keyboard.nextInt();
        return customerId;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return discount, the discount of the order or partOrder. 
     */

    private double inputDiscount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast discount %: ");
        double discount = keyboard.nextDouble();
        return discount;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return quantity, the quantity of the item on the partOrder.
     */

    private int inputQuantity()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Antal: ");
        int quantity = keyboard.nextInt();
        return quantity;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return barCode, the barCode of the item. 
     */

    private String inputBarCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast/scan stregkode: ");
        String barCode = keyboard.nextLine();
        return barCode;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return finished, ja or nej. 
     */

    private boolean inputFinished()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Færdig? ja/nej: ");
        String done = keyboard.nextLine();
        boolean finished = false;
        if(done.equals("ja"))
        {
            finished = true;
        }
        return finished;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return amount, the amount paid by the customer.
     */

    private double inputAmount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Beløb betalt: ");
        double amount = keyboard.nextDouble();
        return amount;
    }

    /**
     * Prints out acommand that you should type in a number.
     */

    private void printCatch()
    {
        System.out.println("\n Du skal indtaste et tal - prøv igen!");
    }

    /**
     * Writes that you should type retur to return to the menu.
     * 
     * @return input, your answer to the print. 
     */

    private String returnToMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Tryk på vilkårlig tast for at komme tilbage til hoved menu: ");
        String input = keyboard.nextLine();
        return input;
    }

}
